package com.linkoog.devtools.code_generator.util;

import javax.swing.*;
import javax.swing.tree.TreePath;

/**
 * @author wujiazhen
 * @Date 2019/1/15
 */
public class JTreeUtils {

    public static void expandTree(JTree myTree) {
        expandSubTree(myTree,  new TreePath(myTree.getModel().getRoot()) );
    }

    private static void expandSubTree(JTree myTree, TreePath path) {
        if (path == null) {
            return;
        }
        Object lastPathComponent = path.getLastPathComponent();
        int childCount = myTree.getModel().getChildCount(lastPathComponent);
        for (int i = 0; i < childCount; i++) {
            expandSubTree(myTree, path.pathByAddingChild(myTree.getModel().getChild(lastPathComponent, i)));
        }
        myTree.expandPath(path);
    }
}
